﻿using System;
using Gencode.Gengui;
using System.Data;
using Gencode.PrsCommon.Data.Plan;
using System.Windows.Forms;
using System.Drawing;
using Gencode.PrsCommon.Data.Person;
using Gencode.PrsCommon.Data.Abteilung;

namespace Gencode.PrsUtil.Controls
{
    public class PUSchemaGrid : GUGrid
    {
        DataView m_schemas;
        private System.Windows.Forms.DataGridViewTextBoxColumn id;
        private System.Windows.Forms.DataGridViewTextBoxColumn name;
		string mandid;
		bool onlyActive;

        public PUSchemaGrid()
        {
			if (Columns.Count > 0)
				Columns.Clear();

            InitializeCompnent();
        }

        private void InitializeCompnent()
        {
            this.id = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.name = new System.Windows.Forms.DataGridViewTextBoxColumn();

            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.id,
            this.name});

            // 
            // nachname
            // 
            this.id.HeaderText = "ID";
            this.id.Name = "id";
            this.id.ReadOnly = true;
            this.id.Width = 84;
            // 
            // abteilung
            // 
            this.name.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.name.HeaderText = "Name";
            this.name.Name = "name";
            this.name.ReadOnly = true;
            this.name.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;

            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
        }

        public void ReloadData(string mandid, bool onlyActive)
        {
            if (m_schemas != null)
                m_schemas.Dispose();

            m_schemas = PlanData.GetSchemas(mandid, onlyActive);
            AutoGenerateColumns = false;
            DataSource = m_schemas;

            Columns["id"].DataPropertyName = "schemaid";
            Columns["name"].DataPropertyName = "name";

			SetStatusColor();

			this.mandid = mandid;
			this.onlyActive = onlyActive;

			if (Rows.Count > 0)
				Rows[0].Selected = false;
        }

		private void SetStatusColor()
		{
			if (m_schemas != null)
			{
				for (int i = 0; i < m_schemas.Count; i++)
				{
					if (int.Parse(m_schemas[i].Row["status"].ToString()) == 0)
					{
						this.Rows[i].Cells[0].Style.BackColor = Color.Red;
						this.Rows[i].Cells[1].Style.BackColor = Color.Red;
					}
				}
			}
		}

		public void ChangeSchemaStatus(int rowindex)
		{
			int status = 1;
			if(int.Parse(m_schemas[rowindex].Row["status"].ToString()) == 1)
				status = 0;
			PlanData.UpdateSchemaStatus(int.Parse(m_schemas[rowindex].Row["schemaid"].ToString()), status);

			ReloadData(mandid, onlyActive);
		}

		public int SchemaID
		{
			get
			{
                if (m_schemas.Count <= 0)
                    return 0;
				return int.Parse(m_schemas[SelectedRows[0].Index].Row["schemaid"].ToString());
			}
		}
	}


    public class PUPlanSchemaGrid : GUGrid
    {
        DataView m_schemagrid;

        private System.Windows.Forms.DataGridViewImageColumn up;
		//private System.Windows.Forms.DataGridViewButtonColumn up;
        private System.Windows.Forms.DataGridViewImageColumn down;
		//private System.Windows.Forms.DataGridViewButtonColumn down;
        private System.Windows.Forms.DataGridViewTextBoxColumn position;
        private System.Windows.Forms.DataGridViewTextBoxColumn prsid;
        private System.Windows.Forms.DataGridViewTextBoxColumn vorname;
        private System.Windows.Forms.DataGridViewTextBoxColumn nachname;
        private System.Windows.Forms.DataGridViewTextBoxColumn abteilung;

        public PUPlanSchemaGrid()
        {
			if (Columns.Count > 0)
				Columns.Clear();

            InitializeComponent();
        }

        protected void InitializeComponent()
        {
            //this.up = new System.Windows.Forms.DataGridViewButtonColumn();
			this.up = new System.Windows.Forms.DataGridViewImageColumn();
            //this.down = new System.Windows.Forms.DataGridViewButtonColumn();
			this.down = new System.Windows.Forms.DataGridViewImageColumn();
            this.position = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.prsid = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.vorname = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nachname = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.abteilung = new System.Windows.Forms.DataGridViewTextBoxColumn();

            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.up,
            this.down,
            this.position,
            this.prsid,
            this.vorname,
            this.nachname,
            this.abteilung});
            this.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.GridCellContentClick);
            // 
            // up
            // 
            this.up.HeaderText = "";
            this.up.Name = "up";
            this.up.ReadOnly = true;
			//this.up.Text = "^";
			this.up.Image = Gencode.PrsUtil.Properties.Resources.Navigation_2_Up_16;
			//this.up.UseColumnTextForButtonValue = true;
            this.up.Width = 21;
            // 
            // down
            // 
            this.down.HeaderText = "";
            this.down.Name = "down";
            this.down.ReadOnly = true;
			this.down.Image = Gencode.PrsUtil.Properties.Resources.Navigation_2_Down_16;
			//this.down.Text = "v";
			//this.down.UseColumnTextForButtonValue = true;
            this.down.Width = 21;
            // 
            // position
            // 
            this.position.HeaderText = "Position";
            this.position.Name = "position";
            this.position.ReadOnly = true;
            this.position.Width = 69;
            // 
            // prsid
            // 
            this.prsid.HeaderText = "ID";
            this.prsid.Name = "prsid";
            this.prsid.ReadOnly = true;
            this.prsid.Width = 43;
            // 
            // vorname
            // 
            this.vorname.HeaderText = "Vorname";
            this.vorname.Name = "vorname";
            this.vorname.ReadOnly = true;
            this.vorname.Width = 74;
            // 
            // nachname
            // 
            this.nachname.HeaderText = "Nachname";
            this.nachname.Name = "nachname";
            this.nachname.ReadOnly = true;
            this.nachname.Width = 84;
            // 
            // abteilung
            // 
            this.abteilung.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.abteilung.HeaderText = "Abteilung";
            this.abteilung.Name = "abteilung";
            this.abteilung.ReadOnly = true;

            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
        }

        protected override void InitLayout()
        {
            base.InitLayout();
			this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        public void ReloadData(int SchemaID)
        {
            if (m_schemagrid != null)
                m_schemagrid.Dispose();

            m_schemagrid = PlanData.GetPlanSchema(SchemaID);
			BindData();
        }

		private void BindData()
		{
			AutoGenerateColumns = false;
			DataSource = m_schemagrid;
			Columns["position"].DataPropertyName = "prsschemaid";
			Columns["prsid"].DataPropertyName = "prsid";
			Columns["vorname"].DataPropertyName = "vorname";
			Columns["nachname"].DataPropertyName = "nachname";
			Columns["abteilung"].DataPropertyName = "abteilungname";

			SetColor();
		}

        private void GridCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
			if (e.RowIndex < 0)
				return;

            int selectedRow = e.RowIndex;

            //keine abteilungen verschieben
            if (int.Parse(m_schemagrid[e.RowIndex].Row["prsid"].ToString()) != 0)
            {
                DataRow row = m_schemagrid.Table.NewRow();
                object[] arr = m_schemagrid.Table.Rows[e.RowIndex].ItemArray;
                row.ItemArray = arr;

                switch (e.ColumnIndex)
                {
                    case 0: //Up
                        if (e.RowIndex > 0 &&
                            int.Parse(m_schemagrid[e.RowIndex].Row["abteilungid"].ToString()) == int.Parse(m_schemagrid[e.RowIndex - 1].Row["abteilungid"].ToString()) &&
                            int.Parse(m_schemagrid[e.RowIndex - 1].Row["prsid"].ToString()) != 0)
                        {
                            m_schemagrid.Table.Rows.RemoveAt(e.RowIndex);
                            m_schemagrid.Table.Rows.InsertAt(row, e.RowIndex - 1);
							selectedRow--;
                        }
                        break;
                    case 1: //Down
                        if (e.RowIndex < this.Rows.Count - 1 &&
                            int.Parse(m_schemagrid[e.RowIndex].Row["abteilungid"].ToString()) == int.Parse(m_schemagrid[e.RowIndex + 1].Row["abteilungid"].ToString()) &&
                            int.Parse(m_schemagrid[e.RowIndex + 1].Row["prsid"].ToString()) != 0)
                        {
                            m_schemagrid.Table.Rows.RemoveAt(e.RowIndex);
                            m_schemagrid.Table.Rows.InsertAt(row, e.RowIndex + 1);
							selectedRow++;
                        }
                        break;
                }
                this.Rows[selectedRow].Selected = true;
            }
        }

		public void RemoveRowAt(int index)
		{
			//keine abteilungen verschieben
			if (int.Parse(m_schemagrid[index].Row["prsid"].ToString()) != 0)
			{
				m_schemagrid.Table.Rows.RemoveAt(index);
			}
			else
				MessageBox.Show("Abteilungen dürfen nicht entfernt werden!", "Achtung", MessageBoxButtons.OK);
		}

		public int GetAbteilungID(int index)
		{
			return int.Parse(m_schemagrid[index].Row["abteilungid"].ToString());
		}

		public void InsertPersonAt(int index, int prsid, int abteilungid)
		{
			DataView dv = PersonData.GetPerson("", prsid);
			DataView dvabt = AbteilungData.GetAbteilung(abteilungid);

			if (dvabt != null && dvabt.Count > 0)
			{
				if (dv != null && dv.Count > 0)
				{
					DataRow row = m_schemagrid.Table.NewRow();
					row["prsid"] = dv[0].Row["prsid"].ToString();
					row["vorname"] = dv[0].Row["vorname"].ToString();
					row["nachname"] = dv[0].Row["nachname"].ToString();
					row["abteilungid"] = dvabt[0].Row["abteilungid"].ToString();
					row["abteilungname"] = dvabt[0].Row["name"].ToString();
					row["prsschemaid"] = "0";
					row["rgb"] = "-1";

					m_schemagrid.Table.Rows.InsertAt(row, index);
				}
				else //Leerzeile
				{
					DataRow row = m_schemagrid.Table.NewRow();
					row["prsid"] = "-1";
					row["vorname"] = "";
					row["nachname"] = "";
					row["abteilungid"] = dvabt[0].Row["abteilungid"].ToString();
					row["abteilungname"] = dvabt[0].Row["name"].ToString();
					row["prsschemaid"] = "0";
					row["rgb"] = "-1";

					m_schemagrid.Table.Rows.InsertAt(row, index);
				}
			}
		}

		public void AddAbteilungToSchema(int abteilungid)
		{
			DataView dv = PlanData.GetPersonenInAbteilung(abteilungid);
			if (m_schemagrid != null && m_schemagrid.Count > 0)
			{
				for (int i = 0; i < dv.Count; i++)
				{
					DataRow row = m_schemagrid.Table.NewRow();
					object[] arr = dv.Table.Rows[i].ItemArray;
					row.ItemArray = arr;

					m_schemagrid.Table.Rows.Add(row);
					row.EndEdit();
				}
				m_schemagrid.EndInit();
				
			}
			else
				m_schemagrid = dv;

			//dv.Dispose();
			BindData();
		}

		public void AddColorToRowAt(int index, Color color)
		{
			m_schemagrid[index].Row["rgb"] = color.ToArgb().ToString();
			SetColor();
		}

		private void SetColor()
		{
			for (int i = 0; i < m_schemagrid.Count; i++)
			{
				if (int.Parse(m_schemagrid[i].Row["rgb"].ToString()) != -1 &&
					int.Parse(m_schemagrid[i].Row["rgb"].ToString()) != 0)
				{
					this.Rows[i].Cells[0].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
					this.Rows[i].Cells[1].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
					this.Rows[i].Cells[2].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
					this.Rows[i].Cells[3].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
					this.Rows[i].Cells[4].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
					this.Rows[i].Cells[5].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
					this.Rows[i].Cells[6].Style.BackColor = Color.FromArgb(int.Parse(m_schemagrid[i].Row["rgb"].ToString()));
				}

				if (int.Parse(m_schemagrid[i].Row["prsid"].ToString()) == 0)
				{
					((DataGridViewImageCell)this.Rows[i].Cells[0]).Value = Gencode.PrsUtil.Properties.Resources.Navigation_transparent;
					((DataGridViewImageCell)this.Rows[i].Cells[1]).Value = Gencode.PrsUtil.Properties.Resources.Navigation_transparent;
				}
			}
		}
	}
}
